package ar.edu.itba.twiteros.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import ar.edu.itba.twiteros.domain.UserIds;

public class UserDao {
	

	public static void save(UserIds user){
		EntityManager em = EMF.get().createEntityManager();
        try {
        	if(user.getId()==null){
        		em.persist(user);
        	}else{
        		em.getTransaction().begin();
        	    UserIds userx = em.find(UserIds.class, user.getId());
        	    userx.setToken(user.getToken()); 
        	    userx.setTokenSecret(user.getTokenSecret());
        	    userx.setAccess(user.isAccess());
        	    em.getTransaction().commit();
        	}
        }catch(Exception e){
        	em.close();
        	
        }finally {
            em.close();   
        }
        
	}
	
	public static UserIds getUser(Long uid){		
		EntityManager em= EMF.get().createEntityManager();
		UserIds user = null;
		
		Query query = em.createQuery("select from "+UserIds.class.getName()+" where uid = :uid");
		query.setParameter("uid", uid);
		
	    List<UserIds> users = (List<UserIds>)query.getResultList();
	    if (users!=null && !users.isEmpty()) {
	    	user= users.get(0);
	    }
		return user;
	}
}
